package com.training1.api.controller;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.training1.core.dto.APIResultDto;
import com.training1.core.modules.service.MemberService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

@Api(value = "/api/updLoginPassword", description = "修改会员登录密码") // Swagger annotation
@RestController
@RequestMapping("/api/updLoginPassword")
public class UpdLoginPasswordController {
	
	@Resource
	private MemberService memberService;
	
	@ApiOperation(value = "获取该所有消息")
	@RequestMapping(value = "/update", method = RequestMethod.DELETE)
	public APIResultDto<Object> updLoginPasswordController(@RequestParam String cellphone, @RequestParam String oldPassword, 
			@RequestParam String newPassword, @RequestParam String confirmPassword){
		APIResultDto<Object> result =new APIResultDto<>();
		Pattern p=Pattern.compile(".{6,16}");
		Matcher m1=p.matcher(cellphone);
		Matcher m2=p.matcher(oldPassword);
		Matcher m3=p.matcher(newPassword);
		Matcher m4=p.matcher(confirmPassword);
		result.setStatus(APIResultDto.SUCCESS_OK);
		if(m1.matches()&&m2.matches()&&m3.matches()&&m4.matches()){
			if(oldPassword.equals(newPassword)){
				result.setErrorCode("2");
				result.setMessage("新密码不能与原密码相同");
				return result;
			}else{
				if(newPassword.equals(confirmPassword)){
					String flg=memberService.findFlg(cellphone,UpdLoginPasswordController.encode(oldPassword));
					if(flg==null||flg.length()<=0){		
						result.setErrorCode("1");
						result.setMessage("输入的用户名或密码不正确");
						return result;
					}else if(flg=="1"||flg.equals("1")){
						result.setErrorCode("2");
						result.setMessage("您的账号已被锁定,请联系管理员");
						return result;
					}else if(flg=="0"||flg.equals("0")){
						memberService.updateLoginPassword(cellphone, UpdLoginPasswordController.encode(oldPassword), UpdLoginPasswordController.encode(newPassword));
						result.setErrorCode("0");
						result.setMessage("密码更新成功");
						return result;
					}else{
						result.setErrorCode("3");
						result.setMessage("密码更新失败");
						result.setStatus(APIResultDto.SYSTEM_ERROR);
						return result;
					}
				}else{
					result.setErrorCode("3");
					result.setMessage("两次密码输入不一致,请重新输入");
					return result;
				}
			}
		}else{
			result.setErrorCode("1");
			result.setMessage("密码必须输入6~16位字符");
			return result;
		}
	}
	//MD5加密字符串
	public static String encode(String pwd) {  
        try {  
            MessageDigest  digest = MessageDigest.getInstance("MD5");  
            byte[]  bytes = digest.digest(pwd.getBytes());  
            StringBuffer sb = new  StringBuffer();  
            for(int i = 0;i<bytes.length;i++){  
                String s = Integer.toHexString(0xff&bytes[i]);  
                  
                if(s.length()==1){  
                    sb.append("0"+s);  
                }else{  
                    sb.append(s);  
                }  
            }  
              
            return sb.toString();  
        } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();  
            throw new RuntimeException("buhuifasheng");  
        }  
    }  

}
